#pragma once
#include"global_var.cpp"
#include"faults.cpp"

class result
{
private:
    std::fstream out;
    std::fstream out2;
    std::mutex m;
    static int count ;
public:
    explicit result(std::string& path) noexcept{
        std::string file = path + ".det";
        out.open(file.c_str(),std::ios::out);
        file = path + ".udet";
        out2.open(file.c_str(),std::ios::out);

    };
    ~result() noexcept{
        out.close();
        out2.close();
    };

    void write(std::vector<fault*>& ) noexcept;
    void predrop_write(std::vector<fault*>& ) noexcept;

};

int result::count = 0;

void result::write(std::vector<fault*>& faults) noexcept
{   
    std::lock_guard<std::mutex> lock(m);

    count += faults.size()-1;

    for(int i=0,sz=faults.size();i<sz;++i)
    {
        if(i!=0)
        {
            std::stringstream ss(faults[i]->equal_faults[0]);
            std::string type,state,location;
            ss>>type>>state>>location;

            if(faults[i]->state == 0)
            {
                out2<<type<<"   ND   "<<location<<std::endl;
                for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                    out2<<faults[i]->equal_faults[j]<<std::endl;
            }
            else if(faults[i]->state == 1)
            {
                out<<type<<"   PT   "<<location<<std::endl;
                for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                    out<<faults[i]->equal_faults[j]<<std::endl;
            }
            else 
            {
                out<<type<<"   DT   "<<location<<std::endl;
                for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                    out<<faults[i]->equal_faults[j]<<std::endl;
            }
        }
        delete faults[i];
    }
    std::cout<<count<<" faults done."<<std::endl;
};

void result::predrop_write(std::vector<fault*>& faults) noexcept
{   
    std::lock_guard<std::mutex> lock(m);

    count += faults.size();

    for(int i=0,sz=faults.size();i<sz;++i)
    {
        
        std::stringstream ss(faults[i]->equal_faults[0]);
        std::string type,state,location;
        ss>>type>>state>>location;

        if(faults[i]->state == 0)
        {
            out2<<type<<"   ND   "<<location<<std::endl;
            for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                out2<<faults[i]->equal_faults[j]<<std::endl;
        }
        else if(faults[i]->state == 1)
        {
            out<<type<<"   PT   "<<location<<std::endl;
            for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                out<<faults[i]->equal_faults[j]<<std::endl;
        }
        else 
        {
            out<<type<<"   DT   "<<location<<std::endl;
            for(int j = 1,sz2 = faults[i]->equal_faults.size(); j < sz2 ; ++j)
                out<<faults[i]->equal_faults[j]<<std::endl;
        }
        delete faults[i];
    }
    std::cout<<count<<" faults done."<<std::endl;
};
